package com.game.doudizhu.controller;

import com.game.doudizhu.model.GameRoom;
import com.game.doudizhu.model.Player;
import com.game.doudizhu.service.GameService;
import com.game.doudizhu.service.GameRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/game")
public class GameController {
    @Autowired
    private GameService gameService;
    
    @Autowired
    private GameRoomService gameRoomService;
    
    @GetMapping("/rooms")
    public List<GameRoom> getRooms() {
        return gameRoomService.getAllRooms();
    }
    
    @PostMapping("/rooms")
    public GameRoom createRoom(@RequestBody Player player) {
        return gameRoomService.createRoom(player);
    }
    
    @PostMapping("/rooms/{roomId}/join")
    public GameRoom joinRoom(@PathVariable Long roomId, @RequestBody Player player) {
        // 这里不做Join 操作，当页面到game.html，在那里通过webSocket出发joinRoom操作，以防止重复的加入
        return gameRoomService.canJoinRoom(roomId, player);
//        return gameRoomService.joinRoom(roomId, player);
    }
    
    @GetMapping("/rooms/{roomId}")
    public GameRoom getRoom(@PathVariable Long roomId) {
        return gameRoomService.getRoom(roomId);
    }
    
    @GetMapping("/rooms/available")
    public List<GameRoom> getAvailableRooms() {
        return gameRoomService.getAvailableRooms();
    }
    
    @PostMapping("/rooms/ai")
    public GameRoom createAIRoom(@RequestBody Player player) {
        return gameRoomService.createAIRoom(player);
    }
} 